/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.PersonControl;
import entity.Actual_User;
import entity.Person;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class NewCustomer extends javax.swing.JPanel {

    /**
     * Creates new form NewCustomer
     */
    private Principal principal = Actual_User.getInstance().getPrincipal();
    private PersonControl pc = new PersonControl();
    
    public NewCustomer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        passwordTF = new javax.swing.JPasswordField();
        repasswordTF = new javax.swing.JPasswordField();
        usernameTF = new javax.swing.JTextField();
        documentTF = new javax.swing.JTextField();
        lastNameTF = new javax.swing.JTextField();
        nameTF = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        newB = new javax.swing.JButton();
        returnB = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Nombres");

        jLabel2.setText("Apellidos");

        jLabel3.setText("Username");

        jLabel4.setText("Password");

        jLabel5.setText("Confirmar password");

        jLabel6.setText("Documento");

        passwordTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordTFKeyPressed(evt);
            }
        });

        repasswordTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                repasswordTFKeyPressed(evt);
            }
        });

        usernameTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usernameTFKeyPressed(evt);
            }
        });

        documentTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                documentTFKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                documentTFKeyTyped(evt);
            }
        });

        lastNameTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                lastNameTFKeyPressed(evt);
            }
        });

        nameTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameTFKeyPressed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
        jLabel7.setText("NUEVO CLIENTE");

        newB.setText("Siguiente");
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        returnB.setText("Atrás");
        returnB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(jLabel7))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(newB)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(29, 29, 29)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel4))
                                    .addGap(60, 60, 60))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(returnB)
                                        .addComponent(jLabel5))
                                    .addGap(18, 18, 18)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(documentTF)
                                .addComponent(lastNameTF)
                                .addComponent(passwordTF)
                                .addComponent(repasswordTF)
                                .addComponent(usernameTF)
                                .addComponent(nameTF, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)))))
                .addContainerGap(133, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(documentTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(repasswordTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newB)
                    .addComponent(returnB))
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void returnBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnBActionPerformed
        principal.logout();
    }//GEN-LAST:event_returnBActionPerformed

    private void newBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newBActionPerformed
        String name = nameTF.getText();
        String document =  documentTF.getText();
        String lastName = lastNameTF.getText();
        String username = usernameTF.getText();
        String password = String.valueOf(passwordTF.getPassword());
        String repassword = String.valueOf(repasswordTF.getPassword());
        if(!password.equals(repassword)){
            JOptionPane.showMessageDialog(this, "las contraseñas no coinciden", "Advertencia",JOptionPane.WARNING_MESSAGE);
            return;
        }
        Person user = new Person();
        user.setName(name);
        user.setLastName(lastName);
        user.setDocument(document);
        user.setUserName(username);
        user.setPassword(password);
        int res = pc.createUser(user);
        
        if(res == 0){               
            nameTF.setText("");
            passwordTF.setText("");
            repasswordTF.setText("");
            documentTF.setText("");
            JOptionPane.showMessageDialog(this, "El usuario ha sido creado correctamente");
            principal.setCustomer(this);
            principal.acreditar(user);
        
        }
        else if(res == -1){
            JOptionPane.showMessageDialog(this,"El nombre de usuario debe tener entre 5 y 20 caracteres" , "Advertencia",JOptionPane.WARNING_MESSAGE);
            
        }
        else if(res == -2){
            JOptionPane.showMessageDialog(this,"La contraseña debe tener entre 5 y 30 caracteres" , "Advertencia",JOptionPane.WARNING_MESSAGE);
        }        
        else if(res == -3){
            JOptionPane.showMessageDialog(this,"El nombre de usuario ya existe" , "Advertencia",JOptionPane.WARNING_MESSAGE);
        }
        else if(res == -4){
            JOptionPane.showMessageDialog(this,"El documento debe tener entre 7 y 12 caracteres" , "Advertencia",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_newBActionPerformed

    private void nameTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTFKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == '\n') {
            lastNameTF.requestFocus();
        }
    }//GEN-LAST:event_nameTFKeyPressed

    private void lastNameTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lastNameTFKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == '\n') {
            documentTF.requestFocus();
        }
    }//GEN-LAST:event_lastNameTFKeyPressed

    private void documentTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_documentTFKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == '\n') {
            usernameTF.requestFocus();
        }
    }//GEN-LAST:event_documentTFKeyPressed

    private void usernameTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameTFKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == '\n') {
            passwordTF.requestFocus();
        }
    }//GEN-LAST:event_usernameTFKeyPressed

    private void passwordTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordTFKeyPressed
        if (evt.getKeyChar() == '\n') {
            repasswordTF.requestFocus();
        }
    }//GEN-LAST:event_passwordTFKeyPressed

    private void repasswordTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_repasswordTFKeyPressed
        if (evt.getKeyChar() == '\n') {
            this.newBActionPerformed(null);
        }
    }//GEN-LAST:event_repasswordTFKeyPressed

    private void documentTFKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_documentTFKeyTyped
        char l = evt.getKeyChar();
        if(!Character.isDigit(l)){
            evt.consume();
        } 
    }//GEN-LAST:event_documentTFKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField documentTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField lastNameTF;
    private javax.swing.JTextField nameTF;
    private javax.swing.JButton newB;
    private javax.swing.JPasswordField passwordTF;
    private javax.swing.JPasswordField repasswordTF;
    private javax.swing.JButton returnB;
    private javax.swing.JTextField usernameTF;
    // End of variables declaration//GEN-END:variables
}
